//
//  ModifyHeadViewController.m
//  GuoBuGaForIphone
//
//  Created by zhangwo on 16/10/31.
//  Copyright © 2016年 GuoBuGa. All rights reserved.
//

#import "ModifyHeadViewController.h"

@interface ModifyHeadViewController ()

@end

@implementation ModifyHeadViewController
@synthesize tableView = _tableView;

- (instancetype)init
{
    self = [super init];
    if (self) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"settingCell"];
        [self.view addSubview:_tableView];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"设置"];
     self.navigationItem.leftBarButtonItem = [DSXUI barButtonWithStyle:DSXBarButtonStyleBack target:self action:@selector(back)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
}

- (void)back{
    if (![self.navigationController popViewControllerAnimated:YES]) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingCell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        cell.textLabel.text = @"修改头像";
        cell.detailTextLabel.text = @"      ";
        UIImageView *headerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, -5, 30, 30)];
        [headerImage sd_setImageWithURL:[NSURL URLWithString:[BJUserStatus sharedStatus].userpic]];
        headerImage.layer.cornerRadius = 15.0;
        headerImage.layer.masksToBounds = YES;
        [cell.detailTextLabel addSubview:headerImage];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"修改密码";
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"修改手机号";
    }
    if (indexPath.row == 3) {
        cell.textLabel.text = @"消息提醒";
//        cell.detailTextLabel.text = @"      ";
        //实例化一个开关控件
        UISwitch *swiView = [[UISwitch alloc] initWithFrame:CGRectMake(SWIDTH-100, 10, 100, 30)];
        //设置ON一边的背景颜色，默认是绿色
        swiView.onTintColor=[UIColor colorWithHexString:@"0x44A5F6"];
        //设置OFF一边的背景颜色，默认是灰色，发现OFF背景颜色其实也是控件”边框“颜色
        swiView.tintColor=[UIColor colorWithHexString:@"0xDCDCDC"];
        //设置滑块颜色
        swiView.thumbTintColor=[UIColor colorWithHexString:@"0xF0F0F0"];
        //设置成开启病设置动画形式出现，也可以直接用[swiView setOn:YES];
        [swiView setOn:YES animated:YES];
        //开关控件默认是关闭的，setOn可以默认成打开，所以其实它在显示的时候有一个打开的动作，但这个动作不发送消息，即捕捉不到
        //如我们做个试验，弄个针对“值变动”所引发的操作，即值发生变动，开启输出“On”，关闭输出“Off”
        //发现第一次并没有输出On，我们自己手动从关闭到开启时会有On和Off
        //所以这个setOn虽然是一个开启动作，但我们可以认为它相当于是内置了，我们看不到这个动作，所以捕捉不到
        //但这里我们练习了获取值变动病利用isOn来做相应的操作
        [swiView addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
        //isOn是一个getter方法，所以不能够赋值，即不能用swi.isOn=YES，只能取得当前结果，一般用于判断是否是开启状态
        if (swiView.isOn) {
            NSLog(@"开关打开");
        }
        [cell addSubview:swiView];
        
    }
    return cell;
}

//参数传入开关对象本身
- (void) swChange:(UISwitch*) sw{
    
    if(sw.on==YES){
        NSLog(@"开关被打开");
    }else{
        NSLog(@"开关被关闭");
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelected:NO animated:YES];
    if (indexPath.row == 0) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
        [actionSheet addButtonWithTitle:@"拍照"];
        [actionSheet addButtonWithTitle:@"从相册选择"];
        [actionSheet addButtonWithTitle:@"取消"];
        [actionSheet setCancelButtonIndex:2];
        [actionSheet setDelegate:self];
        [actionSheet setTag:101];
        [actionSheet showInView:self.view];
    }
    if (indexPath.row == 1) {
        
    }
    if (indexPath.row == 2) {
        
    }
    if (indexPath.row == 3) {
        
    }
}

#pragma mark - actionsheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 101) {
        UIImagePickerController *pickController = [[UIImagePickerController alloc] init];
        pickController.delegate = self;
        [pickController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
        if (buttonIndex == 0) {
            pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:pickController animated:YES completion:nil];
        }
        if (buttonIndex == 1) {
            pickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:pickController animated:YES completion:nil];
        }
        
    }
}

@end
